package org.anrc.poi;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;

public class MainPreferences extends Activity {
	CheckBox compasView;
	CheckBox locationView;
	Spinner alertRadiusSpinner;
	public static final String USER_PREFERENCES = "USER_PREFERENCES";
	public static final String PREF_COMPAS_VIEW = "PREF_COMPAS_VIEW";
	public static final String PREF_LOCATION_VIEW = "PREF_LOCATION_VIEW";
	public static final String PREF_ALERT_RADIUS = "PREF_ALERT_RADIUS";

	SharedPreferences prefs;
	private int oldAlertRadiusIndex;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main_preferences);

		alertRadiusSpinner = (Spinner) findViewById(R.id.spinner_alert_radius_val);
		compasView = (CheckBox) findViewById(R.id.checkbox_show_view_compas);
		locationView = (CheckBox) findViewById(R.id.checkbox_show_view_location);
		populateSpinners();

		prefs = getSharedPreferences(USER_PREFERENCES, Activity.MODE_PRIVATE);
		updateUIFromPreferences();

		Button okButton = (Button) findViewById(R.id.okButtonMainPref);
		okButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				savePreferences();
				MainPreferences.this.setResult(RESULT_OK);
				finish();
			}
		});
		Button cancelButton = (Button) findViewById(R.id.cancelButtonMainPref);
		cancelButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				MainPreferences.this.setResult(RESULT_CANCELED);
				finish();
			}
		});
	}

	private void populateSpinners() {
		// Populate the update frequency spinner
		ArrayAdapter<CharSequence> fAdapter;
		fAdapter = ArrayAdapter.createFromResource(this,
				R.array.alert_radius_options,
				android.R.layout.simple_spinner_item);
		fAdapter
				.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		alertRadiusSpinner.setAdapter(fAdapter);
	}

	private void updateUIFromPreferences() {
		boolean compasViewChecked = prefs.getBoolean(PREF_COMPAS_VIEW, false);
		boolean locationViewChecked = prefs.getBoolean(PREF_LOCATION_VIEW,
				false);
		oldAlertRadiusIndex = prefs.getInt(PREF_ALERT_RADIUS, 2);
		alertRadiusSpinner.setSelection(oldAlertRadiusIndex);

		compasView.setChecked(compasViewChecked);
		locationView.setChecked(locationViewChecked);

	}

	private void savePreferences() {
		boolean compasViewChecked = compasView.isChecked();
		boolean locationViewChecked = locationView.isChecked();
		int alertRadiusIndex = alertRadiusSpinner.getSelectedItemPosition();

		Editor editor = prefs.edit();
		editor.putBoolean(PREF_COMPAS_VIEW, compasViewChecked);
		editor.putBoolean(PREF_LOCATION_VIEW, locationViewChecked);
		editor.putInt(PREF_ALERT_RADIUS, alertRadiusIndex);

		// jesli zmienil sie promien
		if (oldAlertRadiusIndex != alertRadiusIndex) {
			startService(new Intent(this, PoiRadarService.class));
		}

		editor.commit();
	}

}
